<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Services\BabyService;
use App\Services\CourseService;
use App\Services\CourseContentService;
use App\Services\CourseTypeService;
use App\Services\MaterialService;
use App\Services\TeachingAidService;
use App\Services\TeachingAidsService;
use App\Services\CourseCommentService;
use App\Services\EvaluationService;
use App\Services\CollectionService;
use App\Services\MemberService;
use App\Services\OrderService;
use Session;
use Illuminate\Support\Facades\Redis;

class CourseController extends Controller{
    public function RecommendList($memberid){
        $member = new MemberService;
        $material = new MaterialService;
        $course = new CourseService;
        $userinfo = $member->getMemberInfoByid($memberid);

        # 获取推荐资料信息
        $material_num = $material->getMaterialCount(0);
        $material_list = $material->getMaterialList($material_num,'',0);

        $courselist = $course->getCourseList(0,'',-1,0);

        $_data = array('code'=>'2x000001','material_list'=>$material_list,'userinfo'=>$userinfo,'course_list'=>$courselist);

        return response()->json($_data);
    }
    public function recommendCourseList($code=0){
//        $member = new MemberService;
        $material = new MaterialService;
        $course = new CourseService;

        $userinfo = Session::get('userinfo');

        # 获取推荐资料信息
        $material_num = $material->getMaterialCount(0);
        $material_list = $material->getMaterialList($material_num,'',0);

        $courselist = $course->getCourseList(0,'',-1,0);

        $_data = array('code'=>'2x000001','material_list'=>$material_list,'userinfo'=>$userinfo,'course_list'=>$courselist);

        /*    $baby = new BabyService;
            $eval = new EvaluationService;


            # 获取宝宝信息
            $baby_list = $baby->getBabyList($memberid);

            if(empty($baby_list)){
                $_data = array('code'=>'3x000002','msg'=>'请设置宝宝信息');
            }else{
                if($babyid == 0){
                    $babyid = $baby_list[0]['id'];
                }

                # 获取宝宝测评结果（默认第一个）
                $eval_res = $eval->getEvaluation($memberid, $babyid);

                if (!empty($eval_res)) {
                    $question_id_list = array();
                    foreach($eval_res as $etm=>$est){
                       foreach($est['score_list'] as $key=>$val){
                           $question_id_list[] = $key;
                       }
                    }
                    $tag_list = array_unique($question_id_list);

                    # 获取推荐课程列表（默认第一个宝宝的）
                    $courselist = $course->getCourseListByTag($tag_list);
                    if (!empty($courselist)) {
                        foreach($courselist as $ctm=>$cst){
                            if($cst->thumb == ''){
                                unset($courselist[$ctm]);
                            }
                        }
                        $_data = array('code' => '2x000001', 'course_list' => $courselist, 'material_list' =>
                            $material_list,'baby_id'=>$babyid);
                    } else {
                        $_data = array('code' => '4x000001', 'msg' => '因暂无宝宝测试信息故暂无推荐课程');
                    }
                } else {
                    $_data = array('code' => '3x000003', 'msg' => '暂无宝宝测试结果');
                }
            }
        // }*/
        return response()->json($_data);
    }

    public function courselist($type_id){

        $courseType = new CourseTypeService;
        $course = new CourseService;
        $material = new MaterialService;

        # 根据分类id获取资料信息
        $material_num = $material->getMaterialCount($type_id);
        if ($material_num > 0) {
            $material_list = $material->getMaterialList($material_num, '', $type_id);

            if ($material_list !== false) {
                $_data = array('code' => '2x000001', 'material_list' => $material_list);
            } else {
                $_data = array('code' => '4x000001', 'msg' => '暂无banner信息');
            }
        } else {
            $_data = array('code' => '4x000002', 'msg' => '该分类下焦点图数量为0,请上传');
        }

        # 获取课程列表
        $type_num = $courseType->getCourseTypeCount($type_id);
        $course_lists = $courseType->getCourseTypeList($type_num, '', $type_id);

        if ($course_lists !== FALSE) {
            foreach ($course_lists as $clm => &$clt) {
                //            $course_num = $course->getCourseCount($clt->id);
                $typeId = $clt->id;
                $course_list = $course->getCourseList(0, '', -1, $clt->id);
                if (!empty($course_list)) {
                    $clt->list = $course_list;
                } else {
                    $clt->list = array();
                }
            }
            $_data['course_list'] = $course_lists;
        } else {
            $_data = array('code' => '4x000001', 'msg' => '暂无课程信息');
        }

        return response()->json($_data);
    }

    public function courseMoreList($type_id){

        $course = new CourseService;
        $courseType = new CourseTypeService;

        $typeinfo = $courseType->getCourseTypeById($type_id);
        if(empty($typeinfo)){
            $_data = array('code'=>'4x000001','msg'=>'没有分类信息');
        }else{
            $courselist = $course->getCourseList(0,'',-1,$type_id);
            if(empty($courselist)){
                $_data = array('code'=>'4x000002','msg'=>'该分类下没有课程列表');
            }else{
                $_data = array('code'=>'2x000001','typeinfo'=>$typeinfo,'courselist'=>$courselist);
            }
        }

        return response()->json($_data);
    }

    public function courseinfo($id,$mid=null){
        if($mid != 'null'){
            $course = new CourseService;
            $content = new CourseContentService;
            $comment = new CourseCommentService;
            $collection = new CollectionService;
            $order = new OrderService;
            $member = new MemberService;

            $userinfo = $member->getMemberInfoByid($mid);

            $courseInfo = $course->getCourseInfoById($id);
            if(!empty($courseInfo) && $courseInfo->isfree > 0){
                $orderInfo = $order->getMemberOrderInfo($mid,$courseInfo->id);
                if(!empty($orderInfo) && $orderInfo[0]->pay_status >=40){
                    $_data = array('code'=>'2x000001','msg'=>'已购买');
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'该课程为付费课程，请点击购买课程按钮进行购买');
                }
            }

            $_data['course_info'] = $courseInfo;
            $collectionInfo = $collection->getCollectionInfo($mid,$courseInfo->id,'course');
            $_data['collectionInfo'] = $collectionInfo;
            $course_content_num = $content->getCourseContentNum($id);
            $course_content_list = $content->getCourseContentList($course_content_num,$id);
            foreach($course_content_list as $ctm=>$cst){
                $course_content_list[$ctm]->video_name = '第'.$cst->video_name.'集';
                $course_content_list[$ctm]->video_length = gmdate('i分s秒',$cst->video_length);
            }
            $commentlist = $comment->getCommentList($id);
            $_data['course_content_list'] = $course_content_list;
            $_data['comment_list'] = $commentlist;

            $freeContentInfo = $content->getCourseContentInfoByFree($courseInfo->id);
            $host_uri = 'boom.madao100.com';
            if($freeContentInfo !== false){
                $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
                $_data['course_info']['video_id'] = $freeContentInfo->id;
            }else{
                $orderInfo = $order->getMemberOrderInfo($mid,$courseInfo->id);
                if(!empty($orderInfo)){
                    $firstContentInfo = $course_content_list[0];
                    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
                    $_data['course_info']['video_id'] = $firstContentInfo->id;
                }else{
                    $_data['course_info']['video_id'] = 0;
                }
            }

            $_data['userinfo'] = $userinfo;

        }else{
            if(strpos($_SERVER['REQUEST_URI'],'/null') !== FALSE){
                $request_uri = str_replace('/null','',$_SERVER['REQUEST_URI']);
            }

            $request_uri = str_replace('/','_2f',$request_uri);
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>'http://boomdog.madao100.com/get_code/'.$request_uri);
            // $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }

        return response()->json($_data);
    }

    public function addCollection(){

        if(!intval($_POST['member_id'])){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $collection = new CollectionService;

            date_default_timezone_set('PRC');
            $data['member_id'] = intval($_POST['member_id']);
            $data['type'] = 'course';
            $data['product_id'] = intval($_POST['product_id']);
            $data['add_date'] = date('Y-m-d H:i:s');

            $collectioninfo = $collection->getCollectionInfo(!intval($_POST['member_id']),$data['product_id'],$data['type']);
            if(!empty($collectioninfo)){
                $_data = array('code'=>'3x000001','msg'=>'课程已收藏');
            }else{
                $res = $collection->insertCollection($data);
                if($res){
                    $_data = array('code'=>'2x000001','msg'=>'收藏成功');
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'收藏失败');
                }
            }
        }

        return response()->json($_data);
    }

    public function courseContentInfo($id,$mid=null){
        if($mid == 'null'){
            if(strpos($_SERVER['REQUEST_URI'],'/null') !== FALSE){
                $request_uri = str_replace('/null','',$_SERVER['REQUEST_URI']);
            }

            $request_uri = str_replace('/','_2f',$request_uri);
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>$request_uri);
            // $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $content = new CourseContentService;
            $comment = new CourseCommentService;
            $course = new CourseService;
            $collection = new CollectionService;
            $contentinfo = $content->getCourseContentInfoById($id);

            if(!empty($contentinfo)){
                if($contentinfo->is_free == 1){
                    $order = new OrderService;
                    $orderInfo = $order->getMemberOrderInfo($mid,$contentinfo->curriculum_id);
                    if(!empty($orderInfo) && $orderInfo[0]->pay_status >= 40){
                        $content_num = $content->getCourseContentNum($contentinfo->curriculum_id);
                        $contentlist = $content->getCourseContentList($content_num,$contentinfo->curriculum_id);
                        $commentlist= $comment->getCommentList($contentinfo->curriculum_id);
                        $courseinfo = $course->getCourseInfoById($contentinfo->curriculum_id);
                        $collectionInfo = $collection->getCollectionInfo($mid,$courseinfo->id,'course');
                        $_data = array('code'=>'2x000001','contentinfo'=>$contentinfo,'content_list'=>$contentlist,'commentlist'=>$commentlist,'courseinfo'=>$courseinfo,'collectionInfo'=>$collectionInfo);
                    }else{
                        $_data = array('code'=>'3x000002','msg'=>'该视频为付费视频，请购买后观看');
                    }
                }else{
                    $content_num = $content->getCourseContentNum($contentinfo->curriculum_id);
                    $contentlist = $content->getCourseContentList($content_num,$contentinfo->curriculum_id);
                    $commentlist= $comment->getCommentList($contentinfo->curriculum_id);
                    $courseinfo = $course->getCourseInfoById($contentinfo->curriculum_id);
                    $collectionInfo = $collection->getCollectionInfo($mid,$courseinfo->id,'course');
                    $_data = array('code'=>'2x000001','contentinfo'=>$contentinfo,'content_list'=>$contentlist,
                        'commentlist'=>$commentlist,'courseinfo'=>$courseinfo,'collectionInfo'=>$collectionInfo);
                }
                /*if($contentinfo->is_free == 0){
                    $_data['code'] = '2x000001';

                    $_data['contentinfo'] = $contentinfo;
                    $_data['contentlist'] = $contentlist;
                    $_data['commentlist'] = $commentlist;
                    $_data['courseinfo'] = $courseinfo;
                }else{
                    $userinfo = Session::get('userinfo');
                    if(empty($userinfo)){
                        $_data= array('code'=>'3x000001','msg'=>'请登录');
                    }else{
                        if($userinfo['isbuy'] == '未购买教具'){
                            $_data['code'] = '3x000002';
                            $_data['msg'] = '请购买视频中教具来观看该视频';
                        }else{
                            $content_num = $content->getCourseContentNum($contentinfo->curriculum_id);
                            $contentlist = $content->getCourseContentList($content_num,$contentinfo->curriculum_id);
                            $commentlist= $comment->getCommentList($contentinfo->curriculum_id);
                            $courseinfo = $course->getCourseInfoById($contentinfo->curriculum_id);

                            $_data = array('code'=>'2x000001','contentinfo'=>$contentinfo,'content_list'=>$contentlist,'commentlist'=>$commentlist,'courseinfo'=>$courseinfo);
                        }
                    }
                }*/
            }else{
                $_data = array('code'=>'4x000001','msg'=>'暂无视频信息');
            }
        }
        return response()->json($_data);
    }

    public function addComment(){

        date_default_timezone_set('PRC');
        $comment = new CourseCommentService;

        $data['member_id'] = intval($_POST['member_id']);
        $data['curriculum_id'] = intval($_POST['curriculum_id']);
        $data['content'] = addslashes($_POST['content']);
        $data['add_date'] = date('Y-m-d H:i:s');
        $data['isshow'] = 1;

        $res = $comment->insertComment($data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'留言添加成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'留言添加失败');
        }

        return response()->json($_data);
    }
}